using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;


using System.Data.Linq;
using System.Linq;

using DataLayer;

namespace BusinessLayer.Activities
{
   public partial class Delete_Tab : System.Workflow.ComponentModel.Activity
   {
      public Delete_Tab()
      {
			//InitializeComponent();
      }

      private static DependencyProperty TabIdProperty = DependencyProperty.Register("TabId", typeof(int), typeof(Delete_Tab));
      public int TabId
      {
         get { return (int)base.GetValue(TabIdProperty); }
         set { base.SetValue(TabIdProperty, value); }
      }

      private static DependencyProperty UserGuidProperty = DependencyProperty.Register("UserGuid", typeof(Guid), typeof(Delete_Tab));
      public Guid UserGuid
      {
         get { return (Guid)base.GetValue(UserGuidProperty); }
         set { base.SetValue(UserGuidProperty, value); }
      }


      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
      {
         using (var db = DatabaseHelper.GetDashboardData())
         {
            var usertabs = from p in db.UserTabs
                           where p.TabId == TabId
                           select p;

            foreach (var usertab in usertabs)
            {
               /*
                * if user has more than 1 tab, then jus set the active
                * tab to the next available one
                * otherwise create a tab for him then
                * set it as the active tab
                */
               if (usertab.UserID == UserGuid)
               {
                  //if i'm the one request the change, i will be sending a change active tab request, so no need to bother
               }
               else
               {
                  var TabsOfUser = (from p in db.UserTabs
                                    where p.TabId != TabId && p.UserID == usertab.UserID
                                    orderby p.Order
                                    select p.TabId);

                  if (TabsOfUser.Count() > 1)
                  {
                     usertab.aspnet_User.UserSetting.ActiveTab = TabsOfUser.First();
                  }
                  else
                  {
                     //Code from create_tab
                     var newTab = new Tab();
                     newTab.UserId = UserGuid;
                     newTab.Title = "New Page";
                     newTab.CreatedDate = DateTime.Now;
                     newTab.LastUpdate = DateTime.Now;
                     newTab.PanelWidth_1 = 33;
                     newTab.PanelWidth_2 = 34;
                     newTab.PanelWidth_3 = 33;
                     newTab.PanelWidth_4 = 0;
                     newTab.Icon = "";
                     DatabaseHelper.Insert<Tab>(newTab);

                     //link user to this tab
                     var newUserTab = new UserTab();
                     newUserTab.AccessLevel = 0;
                     newUserTab.TabId = newTab.ID;
                     newUserTab.UserID = UserGuid;
                     //default guid is the individual role, no user belongs to this role
                     newUserTab.RoleID = Constants.Role_Individual;
                     newUserTab.Deleted = false;
                     newUserTab.Order = 0;

                     DatabaseHelper.Insert<UserTab>(newUserTab);

                     usertab.aspnet_User.UserSetting.ActiveTab = newTab.ID;
                  }
               }

               db.Tabs.DeleteOnSubmit(usertab.Tab);

               //no need to update the tab_share tables because of the foreign table links
            }

            db.SubmitChanges();
         }
         return ActivityExecutionStatus.Closed;
      }
   }
}